package com.xuxin.baby.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.xuxin.baby.R;
import com.xuxin.baby.base.BaseActivity;
import com.xuxin.baby.base.BasePagerAdapter;
import com.xuxin.baby.fragment.ReaderFragment;
import com.xuxin.baby.fragment.VideoFragment;
import com.xuxin.smartTabLayout.SlidingTabLayout;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by xu_xin
 * Created on 2020.08.27 0027
 * 视频文章
 */
public class ReaderVideoSlidingActivity extends BaseActivity {
    @BindView(R.id.title_left)
    ImageView title_left;
    @BindView(R.id.title_right)
    ImageView title_right;
    @BindView(R.id.title_name)
    TextView title_name;
    @BindView(R.id.sliding_stl)
    SlidingTabLayout sliding_stl;
    @BindView(R.id.sliding_pager)
    ViewPager sliding_pager;
    @BindView(R.id.search)
    EditText search;
    private final ArrayList<Fragment> mFragments = new ArrayList<>();
    private final String[] mTitles = {"推荐课程", "推荐文章"};
    private VideoFragment videoFragment;
    private ReaderFragment readerFragment;

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_sliding);
    }

    @Override
    public void initView() {
        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            String title = bundle.getString("title");
            String categoryId = bundle.getString("categoryId");
            String keywords = bundle.getString("keywords");
            String ageRge = bundle.getString("ageRge");
            LogUtils.e("ageRge = " + ageRge);
            title_name.setText(title);
            if (!StringUtils.isEmpty(keywords)) {
                search.setText(keywords);
            }
            videoFragment = VideoFragment.newInstance(categoryId, keywords, ageRge);
            readerFragment = ReaderFragment.newInstance(categoryId, keywords, ageRge);
            mFragments.add(videoFragment);
            mFragments.add(readerFragment);
            BasePagerAdapter myAdapter = new BasePagerAdapter(getSupportFragmentManager(), mFragments, mTitles);
            sliding_pager.setAdapter(myAdapter);
            sliding_stl.setViewPager(sliding_pager);
            search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView text, int actionId, KeyEvent event) {
                    if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                        KeyboardUtils.hideSoftInput(ReaderVideoSlidingActivity.this);
                        String keywords = text.getText().toString();
                        if (!StringUtils.isEmpty(keywords)) {
                            videoFragment.searchKeywords(keywords);
                            readerFragment.searchKeywords(keywords);
                        }
                    }
                    return false;
                }
            });
        }
    }

    @Override
    public void initData() {
    }

    @Override
    public void onComplete() {

    }

    @Override
    @OnClick({R.id.title_left})
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.title_left) {
            finish();
        }
    }
}